@echo off
echo Building HomeBox ARMv7 with frontend assets...
echo.

REM 检查Go环境
go version >nul 2>&1
if errorlevel 1 (
    echo Error: Go is not installed or not in PATH
    echo Please install Go from https://golang.org/dl/
    pause
    exit /b 1
)

REM 检查pnpm环境
pnpm --version >nul 2>&1
if errorlevel 1 (
    echo Error: pnpm is not installed
    echo Please install pnpm from https://pnpm.io/installation
    pause
    exit /b 1
)

REM 构建前端
echo Building frontend...
cd frontend
pnpm run build
if errorlevel 1 (
    echo Frontend build failed
    pause
    exit /b 1
)
cd ..

REM 复制前端文件到后端
echo Copying frontend assets...
if exist "backend\app\api\static\public" rmdir /s /q "backend\app\api\static\public"
mkdir "backend\app\api\static\public"
xcopy /s /e /y /q "frontend\.output\public\*" "backend\app\api\static\public\" >nul

REM 设置环境变量并构建
echo Building ARMv7 binary...
cd backend
set CGO_ENABLED=0
set GOOS=linux
set GOARCH=arm
set GOARM=7
go build -tags "nodynamic" -ldflags="-s -w" -o ../dist/homebox-linux-armv7 ./app/api
if errorlevel 1 (
    echo Build with tags failed, trying minimal build...
    go build -ldflags="-s -w" -o ../dist/homebox-linux-armv7 ./app/api
    if errorlevel 1 (
        echo Build failed! See error above.
        pause
        exit /b 1
    )
)

echo.
echo Build completed successfully!
echo Output: dist\homebox-linux-armv7
pause